package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询套餐管理
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation(value = "分页查询套餐管理")
    @GetMapping("/page")
    public Result<PageResult> PageQuery(SetmealPageQueryDTO setmealPageQueryDTO)  {
        log.info("分页查询套餐管理,参数:{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation(value = "新增套餐")
    @PostMapping
    @CacheEvict(value = "setmealCache", key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO)  {
        log.info("新增套餐,参数:{}",setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售/停售
     * @param status
     * @param id
     * @return
     */
    @ApiOperation(value = "套餐起售/停售")
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result updateStatus(@PathVariable Integer status,Long id)  {
        log.info("套餐起售/停售,参数:{}",status,id);
        setmealService.updateStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id)  {
        log.info("根据id查询套餐,参数:{}",id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation(value = "修改套餐")
    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO)  {
        log.info("修改套餐,参数:{}",setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除套餐")
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result deleteSetmeal(@RequestParam List<Long> ids)  {
        log.info("删除套餐,参数:{}",ids);
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }
}
